var app = getApp();
// https://geek-team.xin/schedule/wx_getNavList?tId=050102
Page({
  data: {
    isStudent: null,
    hidden: false,
    curNav: 1,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    navList: null,
    courseList: null,
    sId: null,
    // 传到下一个页面需要的
    courseID: null,
    time: null
  },
  loadingChange() {
    setTimeout(() => {
      this.setData({
        hidden: true
      });
    }, 2000);
  },
  upper: function (e) {
    console.log(e);
  },
  lower: function (e) {
    console.log(e);
  },
  scroll: function (e) {
    console.log(e);
  },
  tap: function (e) {
    for (var i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1]
        });
        break;
      }
    }
  },
  tapMove: function (e) {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    });
  },

  selectNav(event) {
    let id = event.target.dataset.id,
      index = parseInt(event.target.dataset.index);

    var that = this;
    this.setData({
      curNav: id,
      curIndex: index,
      courseList: that.data.navList[index].courseList,
      courseID: that.data.navList[index].cChooseId,
      time: that.data.navList[index].courseList[index].time
    });
  },

  selectDish(event) {
    let couseID = event.currentTarget.dataset.couseID;
    let flag = true;
    let cart = this.data.cart;

    if (cart.length > 0) {
      cart.forEach(function (item, index) {
        if (item == couseID) {
          cart.splice(index, 1);
          flag = false;
        }
      });
    }
    if (flag) cart.push(dish);
    this.setData({
      cartTotal: cart.length
    });
  },
  onLoad() {
    this.loadingChange();
    var that = this;
    var _isStudent = app.globalData.isStudent;
    console.log("fa" + _isStudent)
    this.setData({
      isStudent: _isStudent
    });
    if (_isStudent) {
      getCorseListofStu(that);
    } else {
      getCorseListofteacher(that);
      console.log("fa")
    }
  }
});

function getCorseListofStu(that) {
  if (app.globalData.sId) {
    if (app.globalData.isStudent) {
      wx.request({
        url: "https://geek-team.xin/schedule/wx_getNavListBySid",
        data: {
          sId: app.globalData.sId
        },
        success(e) {
          console.log(e);
          that.setData({
            navList: e.data,
            courseList: e.data[0].courseList,
            courseID: e.data[0].cChooseId
          });
        }
      });
    }
  }
}

function getCorseListofteacher(that) {
  wx.request({
    url: "https://geek-team.xin/schedule/wx_getNavList",
    data: {
      tId: app.globalData.sId
    },
    success(e) {
      console.log(e);
      that.setData({
        navList: e.data,
        courseList: e.data[0].courseList,
        courseID: e.data[0].cChooseId
      });
    }
  });
}
